```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java Happens-Before规则详解 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 50%, #a855f7 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .rule-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #3b82f6 0%, #6366f1 100%);
        }
        .highlight-box {
            border-left: 4px solid #4f46e5;
            background-color: rgba(79, 70, 229, 0.05);
        }
        .scroll-animate {
            opacity: 0;
            transform: translateY(20px);
            transition: all 0.6s ease;
        }
        .animate-in {
            opacity: 1;
            transform: translateY(0);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0 scroll-animate">
                    <h1 class="text-4xl md:text-5xl font-bold mb-6 leading-tight">理解Java Happens-Before规则</h1>
                    <p class="text-xl opacity-90 mb-8">多线程编程中的内存可见性与操作顺序的黄金法则</p>
                    <div class="flex items-center">
                        <div class="w-12 h-12 rounded-full bg-white bg-opacity-20 flex items-center justify-center mr-4">
                            <i class="fas fa-lock-open text-xl"></i>
                        </div>
                        <p class="text-sm opacity-80">确保线程安全的关键概念</p>
                    </div>
                </div>
                <div class="md:w-1/2 flex justify-center scroll-animate">
                    <div class="w-full max-w-md">
                        <div class="mermaid">
                            graph LR
                            A[线程A] -->|启动| B[线程B]
                            C[volatile写] -->|happens-before| D[volatile读]
                            E[锁释放] -->|happens-before| F[锁获取]
                            G[线程内操作1] -->|happens-before| H[线程内操作2]
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Intro Section -->
    <section class="py-16 px-4">
        <div class="container mx-auto max-w-5xl">
            <div class="bg-white rounded-xl shadow-md p-8 md:p-10 scroll-animate">
                <div class="flex items-center mb-6">
                    <div class="rule-icon rounded-full flex items-center justify-center text-white mr-6">
                        <i class="fas fa-memory text-2xl"></i>
                    </div>
                    <h2 class="text-3xl font-bold">什么是Happens-Before规则?</h2>
                </div>
                <p class="text-lg mb-6">
                    Java的<strong>happens-before</strong>规则是Java内存模型(JMM)中的关键概念，它定义了多线程程序中操作之间的<strong>可见性</strong>和<strong>排序规则</strong>。这些规则确保了一个线程的操作如何在另一个线程中可见，从而保证并发程序的正确执行。
                </p>
                <div class="highlight-box p-6 rounded-lg mb-6">
                    <p class="font-medium text-gray-700 mb-2">通俗理解:</p>
                    <p>happens-before规则就是在告诉我们：线程之间如何正确地"看到"彼此的操作，确保程序按预期工作，而不会因为线程的不一致导致混乱。</p>
                </div>
                <div class="flex flex-wrap -mx-2 mt-6">
                    <div class="w-full md:w-1/2 px-2 mb-4">
                        <div class="bg-gray-50 p-4 rounded-lg h-full">
                            <h3 class="font-bold text-lg text-purple-700 mb-2"><i class="fas fa-eye mr-2"></i>内存可见性</h3>
                            <p>线程对共享变量的操作(读、写)并不总是立即对其他线程可见。happens-before规则确保了某些操作对其他线程可见。</p>
                        </div>
                    </div>
                    <div class="w-full md:w-1/2 px-2 mb-4">
                        <div class="bg-gray-50 p-4 rounded-lg h-full">
                            <h3 class="font-bold text-lg text-purple-700 mb-2"><i class="fas fa-random mr-2"></i>操作顺序</h3>
                            <p>确保操作按特定顺序执行，防止指令重排序导致的问题，尤其是在多线程环境下。</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Rules Section -->
    <section class="py-16 px-4 bg-gray-50">
        <div class="container mx-auto max-w-5xl">
            <h2 class="text-3xl font-bold text-center mb-12 scroll-animate">Happens-Before六大核心规则</h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <!-- 规则1 -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover scroll-animate">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="rule-icon rounded-full flex items-center justify-center text-white mr-4">
                                <i class="fas fa-code text-xl"></i>
                            </div>
                            <h3 class="text-2xl font-bold">程序顺序规则</h3>
                        </div>
                        <p class="text-gray-700 mb-4">
                            在<strong>同一个线程</strong>中，操作的顺序与代码的顺序相同。例如，如果线程A先执行了语句<code>x = 1;</code>，然后执行<code>y = 2;</code>，那么在线程A中<code>x = 1;</code> happens-before <code>y = 2;</code>。
                        </p>
                        <div class="bg-blue-50 p-4 rounded-lg">
                            <code class="text-sm font-mono text-blue-800">
                                int x = 1;<br>
                                int y = 2; // 保证x=1先于y=2执行
                            </code>
                        </div>
                    </div>
                </div>
                
                <!-- 规则2 -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover scroll-animate">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="rule-icon rounded-full flex items-center justify-center text-white mr-4">
                                <i class="fas fa-lock text-xl"></i>
                            </div>
                            <h3 class="text-2xl font-bold">锁规则</h3>
                        </div>
                        <p class="text-gray-700 mb-4">
                            一个<strong>unlock操作</strong> happens-before 其后的<strong>lock操作</strong>。如果线程A释放了一个锁，线程B随后获取了这个锁，那么在线程A释放锁之前的所有操作，对线程B都是可见的。
                        </p>
                        <div class="bg-blue-50 p-4 rounded-lg">
                            <code class="text-sm font-mono text-blue-800">
                                synchronized(obj) {<br>
                                &nbsp;&nbsp;// 线程A的操作<br>
                                }<br>
                                // unlock happens-before 下一个lock<br>
                                synchronized(obj) {<br>
                                &nbsp;&nbsp;// 线程B能看到A的所有操作<br>
                                }
                            </code>
                        </div>
                    </div>
                </div>
                
                <!-- 规则3 -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover scroll-animate">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="rule-icon rounded-full flex items-center justify-center text-white mr-4">
                                <i class="fas fa-bolt text-xl"></i>
                            </div>
                            <h3 class="text-2xl font-bold">volatile变量规则</h3>
                        </div>
                        <p class="text-gray-700 mb-4">
                            对<code>volatile</code>变量的<strong>写操作</strong> happens-before 其后的<strong>读操作</strong>。如果一个线程写入了一个<code>volatile</code>变量，之后其他线程读取这个变量，就一定能够看到这个最新的值。
                        </p>
                        <div class="bg-blue-50 p-4 rounded-lg">
                            <code class="text-sm font-mono text-blue-800">
                                volatile boolean flag = false;<br><br>
                                // 线程A<br>
                                flag = true; // 写操作<br><br>
                                // 线程B<br>
                                if(flag) { // 读操作<br>
                                &nbsp;&nbsp;// 保证看到flag=true<br>
                                }
                            </code>
                        </div>
                    </div>
                </div>
                
                <!-- 规则4 -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover scroll-animate">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="rule-icon rounded-full flex items-center justify-center text-white mr-4">
                                <i class="fas fa-play text-xl"></i>
                            </div>
                            <h3 class="text-2xl font-bold">线程启动规则</h3>
                        </div>
                        <p class="text-gray-700 mb-4">
                            如果线程A启动了线程B，那么线程A对共享变量的操作 happens-before 线程B的任何操作。即新线程能看到启动它的线程中的所有操作。
                        </p>
                        <div class="bg-blue-50 p-4 rounded-lg">
                            <code class="text-sm font-mono text-blue-800">
                                int x = 1;<br><br>
                                Thread t = new Thread(() -> {<br>
                                &nbsp;&nbsp;// 保证看到x=1<br>
                                &nbsp;&nbsp;System.out.println(x);<br>
                                });<br><br>
                                t.start(); // start() happens-before 线程t中的操作
                            </code>
                        </div>
                    </div>
                </div>
                
                <!-- 规则5 -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover scroll-animate">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="rule-icon rounded-full flex items-center justify-center text-white mr-4">
                                <i class="fas fa-stop text-xl"></i>
                            </div>
                            <h3 class="text-2xl font-bold">线程终止规则</h3>
                        </div>
                        <p class="text-gray-700 mb-4">
                            如果线程B执行完了并返回，线程B的所有操作 happens-before 线程A中的任何后续操作。即一个线程能看到另一个线程结束前的所有操作。
                        </p>
                        <div class="bg-blue-50 p-4 rounded-lg">
                            <code class="text-sm font-mono text-blue-800">
                                Thread t = new Thread(() -> {<br>
                                &nbsp;&nbsp;x = 1; // 线程B的操作<br>
                                });<br><br>
                                t.start();<br>
                                t.join(); // join() happens-before 后续操作<br>
                                // 保证看到x=1
                            </code>
                        </div>
                    </div>
                </div>
                
                <!-- 规则6 -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover scroll-animate">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="rule-icon rounded-full flex items-center justify-center text-white mr-4">
                                <i class="fas fa-exchange-alt text-xl"></i>
                            </div>
                            <h3 class="text-2xl font-bold">传递性规则</h3>
                        </div>
                        <p class="text-gray-700 mb-4">
                            如果A happens-before B，且B happens-before C，那么A happens-before C。这种传递性让happens-before关系可以组合使用。
                        </p>
                        <div class="bg-blue-50 p-4 rounded-lg">
                            <code class="text-sm font-mono text-blue-800">
                                // 线程A<br>
                                x = 1; // 操作A<br>
                                lock.unlock(); // 操作B happens-after A<br><br>
                                // 线程B<br>
                                lock.lock(); // 操作C happens-after B<br>
                                y = x; // 操作D happens-after C<br>
                                // 根据传递性，D能看到A的x=1
                            </code>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Application Section -->
    <section class="py-16 px-4">
        <div class="container mx-auto max-w-5xl">
            <div class="bg-white rounded-xl shadow-md overflow-hidden scroll-animate">
                <div class="p-8 md:p-10">
                    <div class="flex items-center mb-6">
                        <div class="rule-icon rounded-full flex items-center justify-center text-white mr-6">
                            <i class="fas fa-lightbulb text-2xl"></i>
                        </div>
                        <h2 class="text-3xl font-bold">实际应用价值</h2>
                    </div>
                    
                    <div class="grid md:grid-cols-2 gap-8">
                        <div>
                            <h3 class="text-xl font-bold mb-4 text-purple-700"><i class="fas fa-shield-alt mr-2"></i>线程安全设计</h3>
                            <p class="mb-6">happens-before规则是设计线程安全类的理论基础。理解这些规则可以帮助开发者：</p>
                            <ul class="space-y-3">
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                    <span>正确实现单例模式(DCL问题)</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                    <span>设计高效的并发数据结构</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                    <span>避免可见性问题导致的bug</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                    <span>优化锁的使用方式</span>
                                </li>
                            </ul>
                        </div>
                        <div>
                            <h3 class="text-xl font-bold mb-4 text-purple-700"><i class="fas fa-project-diagram mr-2"></i>并发模式应用</h3>
                            <div class="mermaid">
                                graph TB
                                A[开始] --> B[单例模式]
                                A --> C[生产者-消费者]
                                A --> D[读写锁]
                                B --> E[双重检查锁定]
                                B --> F[静态内部类]
                                C --> G[BlockingQueue]
                                D --> H[ReadWriteLock]
                                E --> I[volatile变量]
                                G --> J[happens-before保证]
                            </div>
                        </div>
                    </div>
                    
                    <div class="mt-10 p-6 bg-indigo-50 rounded-lg">
                        <h3 class="text-xl font-bold mb-4 text-indigo-700"><i class="fas fa-exclamation-triangle mr-2"></i>常见误区</h3>
                        <div class="grid md:grid-cols-2 gap-6">
                            <div>
                                <div class="flex items-start mb-3">
                                    <i class="fas fa-times-circle text-red-500 mt-1 mr-2"></i>
                                    <div>
                                        <p class="font-medium">认为volatile能替代锁</p>
                                        <p class="text-sm text-gray-600">volatile只能保证可见性，不能保证复合操作的原子性</p>
                                    </div>
                                </div>
                                <div class="flex items-start">
                                    <i class="fas fa-times-circle text-red-500 mt-1 mr-2"></i>
                                    <div>
                                        <p class="font-medium">忽视重排序的影响</p>
                                        <p class="text-sm text-gray-600">没有happens-before关系的操作可能被JVM重排序</p>
                                    </div>
                                </div>
                            </div>
                            <div>
                                <div class="flex items-start mb-3">
                                    <i class="fas fa-times-circle text-red-500 mt-1 mr-2"></i>
                                    <div>
                                        <p class="font-medium">过度依赖线程优先级</p>
                                        <p class="text-sm text-gray-600">线程优先级不提供任何happens-before保证</p>
                                    </div>
                                </div>
                                <div class="flex items-start">
                                    <i class="fas fa-times-circle text-red-500 mt-1 mr-2"></i>
                                    <div>
                                        <p class="font-medium">误解final字段</p>
                                        <p class="text-sm text-gray-600">final字段的可见性有特殊规则，不等于volatile</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Summary Section -->
    <section class="py-16 px-4 bg-gradient-to-r from-indigo-50 to-purple-50">
        <div class="container mx-auto max-w-5xl">
            <div class="bg-white rounded-xl shadow-lg overflow-hidden scroll-animate">
                <div class="md:flex">
                    <div class="md:w-1/3 bg-indigo-600 text-white p-8 flex flex-col justify-center">
                        <div class="text-center">
                            <i class="fas fa-gem text-5xl mb-6"></i>
                            <h3 class="text-2xl font-bold mb-4">核心要点</h3>
                            <p>happens-before关系是Java内存模型的基石，理解它才能编写正确的并发程序</p>
                        </div>
                    </div>
                    <div class="md:w-2/3 p-8">
                        <h3 class="text-2xl font-bold mb-6 text-indigo-700">最佳实践总结</h3>
                        <div class="space-y-6">
                            <div class="flex items-start">
                                <div class="flex-shrink-0 h-10 w-10 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-4">
                                    <i class="fas fa-check"></i>
                                </div>
                                <div>
                                    <h4 class="font-bold">正确使用同步</h4>
                                    <p class="text-gray-600">对于共享变量的访问，要么使用同步(锁)，要么声明为volatile，要么使用不可变对象</p>
                                </div>
                            </div>
                            <div class="flex items-start">
                                <div class="flex-shrink-0 h-10 w-10 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-4">
                                    <i class="fas fa-check"></i>
                                </div>
                                <div>
                                    <h4 class="font-bold">最小化同步范围</h4>
                                    <p class="text-gray-600">在保证正确性的前提下，尽量减少同步块的大小，提高并发性能</p>
                                </div>
                            </div>
                            <div class="flex items-start">
                                <div class="flex-shrink-0 h-10 w-10 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-4">
                                    <i class="fas fa-check"></i>
                                </div>
                                <div>
                                    <h4 class="font-bold">优先使用高级并发工具</h4>
                                    <p class="text-gray-600">如ConcurrentHashMap、CountDownLatch等，它们已经正确实现了happens-before规则</p>
                                </div>
                            </div>
                            <div class="flex items-start">
                                <div class="flex-shrink-0 h-10 w-10 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-4">
                                    <i class="fas fa-check"></i>
                                </div>
                                <div>
                                    <h4 class="font-bold">避免过度优化</h4>
                                    <p class="text-gray-600">在不确定的情况下，保守地使用同步，正确性优于性能</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-10 px-4">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                    <p class="text-sm">深入浅出技术原理</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-indigo-400 hover:text-indigo-300 transition-colors duration-300 flex items-center">
                        <i class="fas fa-external-link-alt mr-2"></i>
                        <span>访问语雀主页</span>
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-center text-sm">
                <p>© 2023 技术小馆. 保留所有权利.</p>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // Scroll animation
        document.addEventListener('DOMContentLoaded', function() {
            const scrollElements = document.querySelectorAll('.scroll-animate');
            
            const elementInView = (el) => {
                const elementTop = el.getBoundingClientRect().top;
                return (
                    elementTop <= (window.innerHeight || document.documentElement.clientHeight)
                );
            };
            
            const displayScrollElement = (element) => {
                element.classList.add('animate-in');
            };
            
            const handleScrollAnimation = () => {
                scrollElements.forEach((el) => {
                    if (elementInView(el)) {
                        displayScrollElement(el);
                    }
                });
            };
            
            // Initialize
            handleScrollAnimation();
            window.addEventListener('scroll', () => {
                handleScrollAnimation();
            });
        });
    </script>
</body>
</html>
```